﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RssController.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   Rss Controller
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.ServiceModel.Syndication;
    using System.Web.Mvc;

    using EcBlog.Data;
    using EcBlog.Models;

    /// <summary>
    /// Rss Controller
    /// </summary>
    [Export, PartCreationPolicy(CreationPolicy.NonShared)]
    public class RssController : Controller
    {
        #region ctor
        private readonly IEcUnitOfWork unitOfWork;

        /// <summary>
        /// Initializes a new instance of the <see cref="RssController"/> class.
        /// </summary>
        /// <param name="unitOfWork">The ec unit of work.</param>
        [ImportingConstructor]
        public RssController(IEcUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        } 
        #endregion

        #region feed
        /// <summary>
        /// Feeds this instance.
        /// </summary>
        /// <returns>The syndication feed result.</returns>
        public SyndicationFeedResult Feed()
        {
            var feed = this.CreateFeed();
            return new SyndicationFeedResult(feed);
        }

        /// <summary>
        /// Creates the feed.
        /// </summary>
        /// <returns>The syndication feed result.</returns>
        public SyndicationFeed CreateFeed()
        {
            var urlSuffix = this.unitOfWork.SiteSettingRepository[SiteSettingKey.UrlSuffix];
            var feed = new SyndicationFeed(
                this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteTitle],
                this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteSlogan],
                new Uri(this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]));
            feed.Categories.Add(new SyndicationCategory("Blog"));
            feed.Language = this.unitOfWork.SiteSettingRepository[SiteSettingKey.FeedLanguage];
            feed.Copyright = new TextSyndicationContent(DateTime.Now.Year + " " + this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteTitle]);
            feed.LastUpdatedTime = DateTime.Now;
            feed.Authors.Add(
                new SyndicationPerson
                    {
                        Name = this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteTitle],
                        Email = this.unitOfWork.SiteSettingRepository[SiteSettingKey.Email]
                    });
            var feedItems = new List<SyndicationItem>();
            var numberOfVisiblePageInRss = int.Parse(this.unitOfWork.SiteSettingRepository[SiteSettingKey.NumberOfVisiblePageInRss]);
            var pages = this.unitOfWork.PageRepository.Get()
                                       .Where(c => c.IsPublish && c.Published < DateTime.Now && c.ShowPostList)
                                       .OrderByDescending(c => c.Published).Take(numberOfVisiblePageInRss);
            foreach (var item in pages)
            {
                var syndicationItem = new SyndicationItem(
                    item.Title,
                    null,
                    new Uri(
                        this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl] + "/post/" + item.Published.Year
                        + "/" + item.Published.Month + "/" + item.Published.Day + "/" + item.Slug + urlSuffix))
                    {
                        Summary = new TextSyndicationContent(item.ContentHtml),
                        Id = item.Slug,
                        PublishDate = item.Created
                    };
                syndicationItem.Links.Add(
                    new SyndicationLink
                        {
                            Title = item.Title,
                            Uri =
                                new Uri(
                                this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl] + "/post/"
                                + item.Published.Year + "/" + item.Published.Month + "/" + item.Published.Day + "/"
                                + item.Slug + urlSuffix),
                            Length = item.ContentHtml.Length,
                            MediaType = "html"
                        });
                feedItems.Add(syndicationItem);
            }

            feed.Items = feedItems;
            return feed;
        } 
        #endregion

        #region Site Map
        /// <summary>
        /// Gets the site map.
        /// </summary>
        /// <returns>the site map</returns>
        public ActionResult SiteMap()
        {
            var urlFormat = this.unitOfWork.SiteSettingRepository[SiteSettingKey.SlugUrlFormat];
            var urlSuffix = this.unitOfWork.SiteSettingRepository[SiteSettingKey.UrlSuffix];
            var pages =
                this.unitOfWork.PageRepository
                    .Get()
                    .Where(c => c.IsPublish && c.Published < DateTime.Now)
                    .OrderByDescending(c => c.Published).ToList();
            return new SitemapActionResult(this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl], pages, urlFormat, urlSuffix);
        }

        #endregion
    }
}